package com.shop.cereshop.app.dl.logic.utils;

import com.github.pagehelper.PageInfo;

import java.util.ArrayList;
import java.util.List;

/**
 * @program: dlshop
 * 描述：对list分页
 * @author: 我好帅
 * @create: 2021-06-23 09:22
 **/
public class ListPageHelper {
    //List分页1
    public static <T> PageInfo<T> startPage(List<T> list, Integer page, Integer row) {
        //创建Page类
        com.github.pagehelper.Page page1 = new com.github.pagehelper.Page(page, row);
        //为Page类中的total属性赋值
        page1.setTotal(list.size());
        //计算当前需要显示的数据下标起始值
        int startIndex = (page - 1) * row;
        int endIndex = Math.min(startIndex + row, list.size());
        //从链表中截取需要显示的子链表，并加入到Page
        page1.addAll(list.subList(startIndex, endIndex));
        //以Page创建PageInfo
        PageInfo pageInfo = new PageInfo<>(page1);
        return pageInfo;
    }

    //List分页2
    public static List pageList(List list, Integer page, Integer row) {
        int limit = row * (page - 1);
        int size = row * page;
        if (list.size() > limit) {
            if (list.size() <= size) {
                size = list.size();
            }
            list = list.subList(limit, size);
            return list;
        }
        return new ArrayList();
    }
    public static boolean isNumeric(String str){
        for (int i = str.length();--i>=0;){
            if (!Character.isDigit(str.charAt(i))){
                return false;
            }
        }
        return true;
    }
}
